//数据库操作
import java.sql.*;
public class Server_Database {
    Connection con;         //用于与数据库建立连接
    static Statement stmt;  //用于发送sql语句
    Server_Database(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");  //驱动信息
            String url = "jdbc:mysql:// 127.0.0.1:3306/game? useSSL=true&characterEncoding=gb2312&serverTimezone=GMT%2B8";
            //？之后是一些参数,不写的话很大几率报错
            con = DriverManager.getConnection(url, "root", "111111");
            stmt = con.createStatement();
            Server_DataWin.area.append("数据库连接成功!\n");
        }catch(Exception e) {
            Server_DataWin.area.append("数据库连接失败!");
        }
        String sql ="create table `玩家信息`"+
                "(`账号` char(20),`密码` char(10),`总场数` int,`总分数` int,`总胜利局数` int,"+
                "primary key(`账号`))";
        largeUpdata(sql);
    }
    static void largeUpdata(String s){  //用于创建表
        try {
            stmt.executeLargeUpdate(s);
        }catch(SQLException e) {}
    }
    static void update(String s) {      //用于更新数据
        try {
            stmt.executeUpdate(s);
        }catch(SQLException e) {}
    }
    static String queryOne(String s) {  //用于查询单一数据
        ResultSet rs = null;
        String s1 = "";
        try {
            rs = stmt.executeQuery(s);
            rs.next();
            s1 = rs.getString(1);
        }catch(Exception e) {}
        return s1;
    }
    
    static String query1(String s) {   //用于查询玩家信息
        ResultSet rs = null;
        String data = "";
        try {
            rs = stmt.executeQuery(s);
            rs.next();
            data = "总场数: "+rs.getString(1)+"\n";
            data = data+"总分数: "+rs.getString(2)+"\n";
            data = data+"赢数: "+rs.getString(3)+"\n";
        }catch(Exception e) {}
        return data;
    }
}